﻿#include "pch.h"

#include <chrono>
#include <random>

#include <omp.h>
#include "Gimer.h"
using std::cout;
using std::endl;
TEST(parallel, p1) {
	int j = 0;
#pragma omp parallel num_threads(9)
	printf("j = % d, ThreadId = % d\n", j, omp_get_thread_num());
}

class base {
public:
	base() {
		cout << "base constructor" << endl;
	}

	virtual ~base() {
		cout << "base destructor" << endl;
	}
};

class derived : public base {
public:
	derived() {
		cout << "derived constructor" << endl;
	}

	~derived() {
		cout << "derived destructor" << endl;
	}
};

class derived1 : public derived {
public:
	derived1() {
		cout << "derived1 constructor" << endl;
	}

	~derived1() {
		cout << "derived1 destructor" << endl;
	}
};

TEST(virutal, v1) {
	base* pBase = new derived1;
	auto uPtr = std::make_unique<derived1>();
	std::cout << "---" << endl;
	delete pBase;
}

int add(int i, int j);

TEST(link, l1) {
	add(1, 2);
	cout << "end" << endl;
}

int add(int i, int j) {
	return i + j;
}

void SortTest(std::vector<int>& is) {
	std::ranges::sort(is.begin(), is.end(), [](const int& x, const int& y) {
		return x > y;
	});
}

TEST(sort, s1) {
	std::vector<int> input = {1, 3, 4, 6, 7, 213, 6, 434};
	SortTest(input);
}

TEST(ForTest, f1) {
	std::vector<int> input = {1, 3, 4, 6, 7, 213, 6, 434};
	SortTest(input);
}

void PushRef(std::vector<int>& vs, const int& i) {
	vs.push_back(i);
}

TEST(vector, v1) {
	int i = 3;
	std::vector<int> vs;
	PushRef(vs, i);
	i = 4;
	for (auto v : vs) {
		cout << "vs: " << v << endl;
	}
	cout << "i:" << i << endl;
}

class VObj {
public:
	int i;
	int j;
	VObj() { }

	VObj(const VObj& other) {
		cout << " vobj copy" << endl;
	}
};

TEST(vector, v2) {
	VObj vo;
	vo.i = 3;
	vo.j = 23;
	std::vector<VObj> vs;
	vs.push_back(std::move(vo));
	// vo.i = 4;
	// vo.j = 78;
	for (const auto& v : vs) {
		cout << "v i: " << v.i << endl;
		cout << "v j: " << v.j << endl;
	}
	// cout << "vo i:" << vo.i << endl;
	// cout << "vo j:" << vo.j << endl;
}

std::vector<double> GenRdNum(int num) {
	// 创建随机数生成器引擎
	std::random_device rd;
	std::mt19937 gen(rd());
	std::uniform_real_distribution<double> dis(0.0, 1000.0); // 在0.0到1.0之间生成浮点数

	// 生成一百个随机浮点数并存储在 vector 中
	std::vector<double> randomFloats(num);

	for (int i = 0; i < num; ++i) {
		randomFloats[i] = dis(gen);
	}
	return randomFloats;
}

TEST(random, r1) {
	// 创建随机数生成器引擎
	std::random_device rd;
	std::mt19937 gen(rd());
	std::uniform_real_distribution<double> dis(0.0, 1000.0); // 在0.0到1.0之间生成浮点数

	// 生成一百个随机浮点数并存储在 vector 中
	std::vector<double> randomFloats(100);

	for (int i = 0; i < 100; ++i) {
		randomFloats[i] = dis(gen);
	}

	// 打印生成的随机浮点数
	std::cout << "Random Floats: ";
	for (const auto& num : randomFloats) {
		std::cout << num << " ";
	}
	std::cout << std::endl;
}

TEST(preformance, p1) {
	auto f1s = GenRdNum(100000);
	auto f2s = GenRdNum(100000);

	for (auto f1 : f1s) {
		// #pragma omp parallel for
		for (auto f2 : f2s) {
			auto f_res = f1 / f2;
			// cout << f_res << endl;
		}
	}
}


TEST(chrono, c1) {
	// 获取当前时间点
	auto start = std::chrono::high_resolution_clock::now();

	// 运行一些代码或等待一段时间

	// 获取另一个时间点
	auto end = std::chrono::high_resolution_clock::now();

	// 计算时间间隔（单位：毫秒）
	auto duration = std::chrono::duration_cast<std::chrono::duration<double, std::milli>>(end - start);

	// 打印时间间隔
	std::cout << "Time elapsed: " << duration.count() << " milliseconds." << std::endl;
}

int int_n(double d, int n) {
	int result = d * pow(10, n); // 保留前4个数
	std::stringstream ss;
	ss << result; // 将整数转换为字符串
	std::string str = ss.str(); // 获取字符串
	auto str_res = str.substr(0, n);
	int result2 = std::stoi(str_res);
	return result2;
}

TEST(intPre, ip1) {
	double num = 1.23456789; // 定义一个整数

	std::cout << int_n(num, 5) << std::endl;

	double num1 = 231.23456789; // 定义一个整数

	std::cout << int_n(num1, 5) << std::endl;

	double num2 = 0.0000001123; // 定义一个整数

	std::cout << int_n(num2, 5) << std::endl;
}
